/*
	File: C:/works/3d/games/mgt/panzer_2010/data/scripts/mobile_item.nut
	Author: Astrofra
*/

		Include("data/scripts/game_item.nut")

/*!
	@short	MobilePlatform
	@author	Astrofra
*/
class	MobilePlatform	extends	GameItem
{
	speed			=	Mtrs(2.0)	//	Actual 'real world' speed of the mobile platform

	item_pos	=	0
	item_pos_0		=	0
	item_pos_1		=	0

	blend_speed		=	0.125
	blend_factor	=	0.0
	blend_dir		=	1.0

	target_item		=	0

	platform_vel	=	0
	prev_pos		=	0

	low_dt_compensation	=	1.0

	/*!
		@short	OnUpdate
		Called during the scene update, each frame.
	*/
	function	OnUpdate(item)
	{
		base.OnUpdate(item)

		blend_factor += (g_dt_frame * blend_speed * blend_dir)

		if ((blend_factor > 1.0) || (blend_factor < 0.0))
		{
			blend_factor = Clamp(blend_factor, 0.0, 1.0)
			blend_dir *= -1.0
		}

		local	_blend_pos = item_pos_0.Lerp(blend_factor, item_pos_1)
		ItemSetPosition(item, ItemGetPosition(item).Lerp(0.75, item_pos + _blend_pos))

		platform_vel = ItemGetWorldPosition(item) - prev_pos
		prev_pos = ItemGetWorldPosition(item)
	}

	function	PlayerIsInsideItem(item)
	{
		ItemApplyLinearImpulse(target_item, platform_vel)
		//ItemApplyLinearForce(target_item, platform_vel.Normalize().Scale(ItemGetMass(target_item)))
	}

	/*!
		@short	OnSetup
		Called when the item is about to be setup.
	*/
	function	OnSetup(item)
	{
		base.OnSetup(item)

		item_pos_0		=	Vector(0,0,0)
		item_pos_1		=	Vector(0,0,0)
		item_pos = ItemGetPosition(item)

		local	_parent
		_parent = ItemGetParent(item)
		if (ObjectIsValid(_parent))
		{
			item_pos_0 = ItemGetPosition(ItemGetChild(_parent, "pos_0"))
			item_pos_1 = ItemGetPosition(ItemGetChild(_parent, "pos_1"))
			blend_speed = speed / item_pos_0.Dist(item_pos_1)
			ItemSetPosition(item, item_pos_0.Lerp(0.5, item_pos_1))
		}


		blend_factor = 0.5
		platform_vel = Vector(0,0,0)
		prev_pos = ItemGetWorldPosition(item)
	}

	function	OnSetupDone(item)
	{
		target_item = SceneFindItem(ItemGetScene(item), "tank")
	}
}
